<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }"
        >小凡自习室</el-breadcrumb-item
      >
      <el-breadcrumb-item :to="{ path: '/welcome' }"
        >vue基础</el-breadcrumb-item
      >
      <el-breadcrumb-item>增删改查</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            :disabled="lookOK"
            @click="dialogVisible = true"
            >添加</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-search"
            style="float: right"
            @click="lookup"
            v-if="lookOK == false"
            >搜索</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-search"
            style="float: right"
            @click="toTable"
            v-else
            >返回全列表</el-button
          >
          <!-- 全部列表 -->
          <el-table :data="tableData" border v-if="lookOK == false">
            <el-table-column
              prop="id"
              label="序号"
              width="80"
              style="text-align: center"
            >
            </el-table-column>
            <el-table-column prop="date" label="日期" width="120">
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="80">
            </el-table-column>
            <el-table-column prop="sex" label="性别" width="80">
            </el-table-column>
            <el-table-column prop="Worth" label="身价" width="100">
            </el-table-column>
            <el-table-column prop="address" label="地址"> </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  @click="ToChenge(scope.row)"
                  icon="el-icon-edit"
                  size="mini"
                ></el-button>

                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="delItem(scope.row.id)"
                  size="mini"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 查找列表 -->
          <el-table :data="lookData" border v-else>
            <el-table-column
              prop="id"
              label="序号"
              width="80"
              style="text-align: center"
            >
            </el-table-column>
            <el-table-column prop="date" label="日期"> </el-table-column>
            <el-table-column prop="name" label="姓名"> </el-table-column>
            <el-table-column prop="sex" label="性别" width="80">
            </el-table-column>
            <el-table-column prop="address" label="地址"> </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  @click="ToChenge(scope.row)"
                  icon="el-icon-edit"
                  size="mini"
                ></el-button>

                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="delItem(scope.row.id)"
                  size="mini"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- 添加表单 -->
      <el-dialog
        title="添加信息"
        center
        :visible.sync="dialogVisible"
        width="50%"
      >
        <!-- @close="resetForm('addFormRef')" -->
        <!-- :before-close="handleClose" -->

        <el-form
          label-width="80px"
          ref="addFormRef"
          :rules="rules"
          :model="addData"
        >
          <!-- ref="form" -->
          <!-- :model="form" -->
          <el-form-item label="姓名" prop="name">
            <el-input
              placeholder="请输入姓名"
              v-model="addData.name"
              style="width: 270px"
            ></el-input>
          </el-form-item>

          <!-- prop="resource" -->
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="addData.sex">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="时间" prop="date">
            <!-- <input type="date" v-model="addData.date" /> -->

            <el-date-picker
              v-model="addData.date"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              prop="date"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="身价" prop="Worth">
            <el-input
              type="number"
              placeholder="请输入身价"
              v-model.number="addData.Worth"
              style="width: 270px"
            ></el-input>
          </el-form-item>

          <el-form-item label="地址" prop="address">
            <!-- prop="desc" -->
            <el-input
              placeholder="请输入地址"
              v-model="addData.address"
              style="width: 400px"
            ></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addbtn('addFormRef')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 查找表单 -->
      <el-dialog title="查找" :visible.sync="lookShow" width="40%">
        <span>查找</span>

        <el-input
          placeholder="请输入姓名"
          v-model="looklook"
          style="width: 400px"
        />

        <span slot="footer" class="dialog-footer">
          <el-button @click="lookShow = false">取 消</el-button>
          <el-button type="primary" @click="lookTo">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改表单 -->
      <el-dialog title="修改信息" :visible.sync="changeShow" width="50%">
        <el-form
          label-width="80px"
          ref="addFormRef"
          :rules="rules"
          :model="changeData"
        >
          <!-- ref="form" -->
          <!-- :model="form" -->
          <el-form-item label="姓名" prop="name">
            <el-input
              placeholder="请输入姓名"
              v-model="changeData.name"
              style="width: 270px"
            ></el-input>
          </el-form-item>

          <!-- prop="resource" -->
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="changeData.sex">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="时间" prop="date">
            <!-- <input type="date" v-model="changeData.date" /> -->

            <el-date-picker
              v-model="changeData.date"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              prop="date"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="身价" prop="Worth">
            <el-input
              type="number"
              placeholder="请输入身价"
              v-model.number="changeData.Worth"
              style="width: 270px"
            ></el-input>
          </el-form-item>

          <el-form-item label="地址" prop="address">
            <!-- prop="desc" -->
            <el-input
              placeholder="请输入地址"
              v-model="changeData.address"
              style="width: 400px"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="changeShow = false">取 消</el-button>
          <el-button type="primary" @click="changeItem">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  created() {
    this.$store.dispatch('gitList')
  },
  data() {
    return {
      lookData: [], //查找时对象
      // 添加对象
      addData: {
        id: '',
        date: '',
        name: '',
        sex: '',
        Worth: '',
        address: '',
        checked: false
      },
      dialogVisible: false, //添加对话框显示与否
      lookShow: false, //查找对话框显示与否
      lookOK: false, //查找与全列表切换
      changeShow: false, //修改表单显示与否
      looklook: '', //查找对象
      changeData: {
        date: '',
        name: '',
        sex: '',
        address: '',
        Worth: '',
        checked: false
      },

      // 表单校验
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        sex: { required: true, message: '请输入性别', trigger: 'change' },

        date: [
          {
            // type: 'date',
            required: true,
            message: '请选择时间',
            trigger: 'change'
          }
        ],
        Worth: [
          {
            required: true,
            message: '请输入身价',
            trigger: 'blur'
          }
        ],
        address: [
          {
            required: true,
            message: '请选择地址',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    ...mapState(['tableData']),
    idLeng() {
      return this.tableData[this.tableData.length - 1].id + 1
    }
  },
  methods: {
    // 增
    addbtn(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return console.log(111)

        // this.tableData.push(this.addData)

        this.addData.id = this.idLeng
        this.$store.commit('addItem', { ...this.addData })
        // console.log(this.addData)
        this.dialogVisible = false
        //清空列表
        this.$refs[formName].resetFields()
      })
    },
    // resetForm(formName) {
    //   console.log(this.addData, 1)
    // },
    // 点击查询
    lookTo() {
      this.lookData = []
      this.tableData.forEach((item) => {
        // 查找包含关键字的对象就加进查询列表
        if (item.name.includes(this.looklook)) {
          // console.log(item)
          this.lookData.push(item)
        }
      })

      // this.lookData = this.tableData.filter((item) => {
      //   return item.name.includes(this.looklook)
      // })
      this.lookOK = true
      this.lookShow = false
      this.looklook = ''
    },
    lookup() {
      this.lookShow = true
    },
    // 返回清空
    toTable() {
      this.lookOK = false
      this.lookData = []
    },
    // 修改框打开
    ToChenge(row) {
      this.changeShow = true
      // console.log(row)
      this.changeData = row
    },
    // 修改数据
    changeItem() {
      // console.log(this.changeData)
      // this.tableData.forEach((item) => {
      //   if (item.id == this.changeData.id) {
      //     item = this.changeData
      //   }
      // })
      this.$store.commit('chengeItem', this.changeData)
      this.changeShow = false
    },
    delItem(id) {
      // console.log(id)
      // this.lookData.splice(id, 1)
      if (this.lookOK) {
        const i = this.lookData.findIndex((e) => e.id === id)
        console.log(i)
        i > -1 && this.lookData.splice(i, 1)
      }

      this.$store.commit('delItem', id)
    }
  }
}
</script>

<style scope>
.el-table {
  width: 100%;
  margin-top: 10px;
}
.el-card {
  margin-top: 10px;
}
.el-col-24 {
  height: 100% !important;
  width: 100% !important;
}
</style>